//
//  ADSegmentView.m
//  MySegment
//
//  Created by Adiljan on 2016/12/6.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "ADSegmentView.h"
#import "ADSegmentTool.h"

@interface ADSegmentView ()

{
    NSMutableArray *arrBtns;
}

@end

@implementation ADSegmentView
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)initView{
    _textSize = 17;
    // 默认颜色
    _baseColor = [ADSegmentTool colorWithHexString:@"f56f19"];
    _hightLightedColor = [ADSegmentTool colorWithHexString:@"30f56f19"];
    arrBtns = [NSMutableArray new
               ];
    self.layer.cornerRadius = 3;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = 1;
    self.layer.borderColor = [_baseColor CGColor];
    
    
}
-(void)setBaseColor:(UIColor *)baseColor{
    _baseColor = baseColor;
    [self updateAllViews];
}
-(void)setHightLightedColor:(UIColor *)hightLightedColor{
    _hightLightedColor= hightLightedColor;
    [self updateAllViews];
}
-(void)setArrTitles:(NSArray *)arrTitles{
    _arrTitles = arrTitles;
    _selectIndex = (int)[_arrTitles count]-1;
    [self updateAllViews];
}
-(void)updateAllViews{
    self.layer.borderColor = [_baseColor CGColor];
    [arrBtns removeAllObjects];
    for (UIView *v  in self.subviews) {
        [v removeFromSuperview];
    }
    int count = (int)[_arrTitles count];
    for (int i=0;i<count;i++) {
        NSString *str = [_arrTitles objectAtIndex:i];
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, i*self.frame.size.width/count, self.frame.size.width/count, self.frame.size.height)];
        // 文字偏上5
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 5, 0)];

        [btn setTitle:str forState:UIControlStateNormal];
        [self addSubview:btn];
        [arrBtns addObject:btn];
        btn.backgroundColor = [UIColor clearColor];
        [btn setTitleColor:_baseColor forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setTitleColor:_baseColor forState:UIControlStateHighlighted];
//        btn.titleLabel.font = FONTWITHSIZE(_textSize);
        btn.contentEdgeInsets = UIEdgeInsetsMake(5.0f, 0.0f, 0.0f, 0.0f);
        btn.tag = i;
        [btn setBackgroundImage:[ADSegmentTool createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[ADSegmentTool createImageWithColor:_baseColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[ADSegmentTool createImageWithColor:_hightLightedColor] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(onBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:(2*i+1)*1.0/(count) constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0/count constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
        
    }
    for (int i=1;i<count;i++) {
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, self.frame.size.height)];
        v.backgroundColor = _baseColor;
        [self addSubview:v];
        v.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:v attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:2.0*i/count constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:v attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:v attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:0 constant:1]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:v attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
    }
    [self setSelectIndex:_selectIndex];
}
-(void)setSelectIndex:(int)selectIndex{
    _selectIndex = selectIndex;
    for (UIButton* btn in arrBtns) {
        btn.selected = btn.tag==_selectIndex;
    }
}
-(void)setStrTitiles:(NSString *)strTitiles{
    _strTitiles = strTitiles;
    self.arrTitles = [strTitiles componentsSeparatedByString:@","];
}
-(void)onBtnClick:(id)sender{
    UIButton *btn = sender;
    [self setSelectIndex:(int)btn.tag];
    if (_ADSegmentClickBlock) {
        _ADSegmentClickBlock((int)btn.tag);
    }
}
-(void)setTextSize:(CGFloat)textSize{
    _textSize = textSize;
//    for (UIAlmasButton *btn in arrBtns) {
//        btn.titleLabel.font = FONTWITHSIZE(textSize);
//    }
}
@end
